<?php 
include_once "../includes/myfunction.php";
include_once "../ini.php";
include_once "../thu_vien/xl_user.php";
include_once "../thu_vien/xl_status.php";
include_once "../thu_vien/xl_comment.php";

$dt_xl_user = new xl_user;
$dt_xl_status = new xl_status;
$dt_xl_comment = new xl_comment;

//kiem tra ton tai nguoi dung
$uid = '';
if(!isset($_GET['id'])){
	$uid = $_SESSION['UID'];
}
else {
	$uid = $_GET['id'];
	$kq = $dt_xl_user->kiem_tra($uid);
	if(!$kq['uid']) {
		$_SESSION['msg'] = 'Lỗi ! Nội dung không khả dụng!';
		$_SESSION['type_msg'] = 'error';
		header("Location: ".$_SERVER['HTTP_REFERER']);
		exit;
	}
}

$user = $dt_xl_user->xem($uid);
$ds_status = $dt_xl_status->get_status($uid);

function get_name($uid){	$dt_xl_user1 = new xl_user;
	$user = $dt_xl_user1->lay_ten($uid);
	return $user['name'];
}
function get_img($uid){
	$dt_xl_user1 = new xl_user;
	$user = $dt_xl_user1->lay_hinh($uid);

	return $user['pic'];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US" xml:lang="en">
<head>
<!--
    Created by Artisteer v3.0.0.35414
    Base template (without user's data) checked by http://validator.w3.org : "This page is valid XHTML 1.0 Transitional"
    -->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />
<title>My Social Network</title>
<link rel="stylesheet" href="../css/style.css" type="text/css" media="screen" />
<!--[if IE 6]><link rel="stylesheet" href="../css/style.ie6.css" type="text/css" media="screen" /><![endif]-->
<!--[if IE 7]><link rel="stylesheet" href="../css/style.ie7.css" type="text/css" media="screen" /><![endif]-->
<link rel="stylesheet" href="../css/mystyle.css" type="text/css" media="screen" />
<script type="text/javascript" src="../css/jquery.js"></script>
<script type="text/javascript" src="../css/script.js"></script>


<script language="javascript" type="text/javascript">
function bt_cm(bt){
	var mang_bt = document.getElementsByName('bt_comment[]');
	var mang_cmment = document.getElementsByName('comment[]');
	var mang_stt_id = document.getElementsByName('dsstt[]');
	var i=0;
	var n = mang_cmment.length;
	var num = bt;
	
	//var post_id = new Array('');
	//var jsArray = [ ];
	
	var fromid = <?php echo $uid; ?>;
	var status_id = mang_stt_id.item(num).value;    
	var comment = mang_cmment.item(num).value;
	var time = new Date().getTime();
	
	var new_comment = document.createElement("div");
	new_comment.id = "new_comment";
	new_comment.className = "new_comment";
	var img_new_cm = document.createElement("div");
	img_new_cm.id = "img_new_cm";
	img_new_cm.className = "f_img";
	img_new_cm.innerHTML ="<img src='../upload/avatar.jpg' />"; //phai lay image tu user
	var ten_new_cm = document.createElement("div");
	ten_new_cm.id = "ten_new_cm";
	ten_new_cm.className = "f_ten";
	ten_new_cm.innerHTML ="<a href='sdfdsfds'><b>" + "<?php echo $uid; ?>" + "</b></a> " + comment;
	var time_new_cm = document.createElement("div");
	time_new_cm.id = "time_new_cm";
	time_new_cm.className = "f_truong";
	time_new_cm.innerHTML ="<a href='sdfdsfds'>" + time + "</a>";
	
	new_comment.appendChild(img_new_cm);
	new_comment.appendChild(ten_new_cm);
	new_comment.appendChild(time_new_cm);

	//var group_status = document.getElementsByName('group_status[]');
	//var div_group = group_status.item(num).getAttention('id');
	document.getElementsByName('ins_new_comm[]').item(num).appendChild(new_comment);
	//div_group.appendChild(new_comment);
	//alert(time);
	makeRequest(fromid,status_id,comment,time,'1');
	
	mang_cmment.item(num).value = '';
	mang_cmment.item(num).focus();
	return true;
	//return true;
}

function view_all_cm(num){
	var mang_stt_id = document.getElementsByName('dsstt[]');
	var status_id = mang_stt_id.item(num).value;
	var all_cmm = document.getElementsByName('ds_cmt[]').item(num).value;
	document.getElementsByName('comment_tb[]').item(num).innerHTML =all_cmm;
	
	//document.getElementsByName('ins_before[]').item(num).innerHTML = all_cmm;
	//div_before.innerHTML = value;
	return true;
}

function get_cm(num){
	var mang_cmment = document.getElementsByName('comment[]');
	mang_cmment.item(num).focus();
	return true;
}

var http_request = false;
function makeRequest(fromid,stt_id,comment,time,type) {
	 
  http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
	 http_request = new XMLHttpRequest();
	 if (http_request.overrideMimeType) {
		// set type accordingly to anticipated content type
		//http_request.overrideMimeType('text/xml');
		http_request.overrideMimeType('text/html');
	 }
  } else if (window.ActiveXObject) { // IE
	 try {
		http_request = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
		try {
		   http_request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	 }
  }
  
  if (!http_request) {
	 alert('Cannot create XMLHTTP instance');
	 return false;
  }
  //document.getElementById('thongbao').innerHTML = '<img src="../images/loading27.gif" align="middle" />';
  http_request.onreadystatechange = alertContents;
  if(type == '1'){
	 
  	http_request.open('GET', 'xl_comment.php?fromid=' + fromid + '&stt_id=' + stt_id + '&comment=' +comment + '&time=' + time  , true);
  }
  http_request.send(null);
}

function alertContents() {
  if (http_request.readyState == 4) {
	 if (http_request.status == 200) {
		result = http_request.responseText;
		if(result == "Yes" )  //co loi
		{
			//document.getElementById('thongbao').innerHTML = '(!) Mã sữa đã tồn tại';
		}
		else
		{
			//document.getElementById('thongbao').innerHTML = '';
		}
	 } else {
		alert('There was a problem with the request.');
	 }
  }
}

</script>

</head>
<body>
<div id="art-page-background-glare">
  <div id="art-page-background-glare-image">
    <div id="art-main">
      <div class="art-sheet">
        <div class="art-sheet-tl"></div>
        <div class="art-sheet-tr"></div>
        <div class="art-sheet-bl"></div>
        <div class="art-sheet-br"></div>
        <div class="art-sheet-tc"></div>
        <div class="art-sheet-bc"></div>
        <div class="art-sheet-cl"></div>
        <div class="art-sheet-cr"></div>
        <div class="art-sheet-cc"></div>
        <div class="art-sheet-body"> 
          <!--BEGIN: HEADER-->
          <?php include "../widgets/header.php"; ?>
          <!--END: HEADER--> 
          <!--BEGIN: NAVIGATION MENU-->
          <?php include "../widgets/nav.php"; ?>
          <!--END: NAVIGATION MENU-->
          
          <div class="art-content-layout">
            <div class="art-content-layout-row">
              <div class="art-layout-cell art-sidebar1">
                <div class="art-vmenublock">
                  <div class="art-vmenublock-tl"></div>
                  <div class="art-vmenublock-tr"></div>
                  <div class="art-vmenublock-bl"></div>
                  <div class="art-vmenublock-br"></div>
                  <div class="art-vmenublock-tc"></div>
                  <div class="art-vmenublock-bc"></div>
                  <div class="art-vmenublock-cl"></div>
                  <div class="art-vmenublock-cr"></div>
                  <div class="art-vmenublock-cc"></div>
                  <div class="art-vmenublock-body"> <img src="<?php echo $user['pic'];?>" width="170"/>
                    <div class="cleared"></div>
                  </div>
                </div>
                <!--BEGIN: VERTICAL MENU-->
                <?php
					   $pos = 'left'; 
					   sidebar($pos);
					   ?>
                <!--END: VERTICAL MENU-->
                
                <div class="cleared"></div>
              </div>
              <div class="art-layout-cell art-content">
                <div class="art-post">
                  <div class="art-post-body">
                    <div class="art-post-inner art-article">
                      <h2 class="art-postheader"><a class="hovered" href="#"><?php echo $user['name'];?></a></h2>
                      <div class="art-postheadericons art-metadata-icons"> <img src="../css/images/postdateicon.png" width="16" height="16" alt="" /> Sinh ngày: <?php echo $user['birthday'];?>
                      <?php if ($user['sex']=='male') { ?>
                        | <img src="../images/<?php echo $user['sex'];?>.png" width="18" height="18" alt="" /> Nam
                      <?php } if ($user['sex']=='female') { ?>
                        | <img src="../images/<?php echo $user['sex'];?>.png" width="18" height="18" alt="" /> Nữ
                       <?php } if ($user['hometown_location']!='') {?>
                       
                        | <img src="../images/home.png" width="20" height="20" alt="" /> Đến từ: <?php echo $user['hometown_location'];?> <?php } if ($user['current_location']!='') {?><br/> <img src="../images/location.png" width="16" height="16" alt="" /> Sống tại: <?php echo $user['current_location'];}?> </div> 
                      <div class="art-postcontent">
                        <table class="table_trang_thai">
                          <tr>
                            <td style="vertical-align: middle;"><textarea id="txtStatus" class="txtstatus" rel="sprofile" style="height: 30px;" name="txtStatus" autocomplete="off" onfocus="if(this.value=='Chia sẻ với bạn bè: Bạn đang làm gì?') this.value='';" onblur="if(this.value=='') this.value='Chia sẻ với bạn bè: Bạn đang làm gì?';" value="">Chia sẻ với bạn bè: Bạn đang làm gì?</textarea></td>
                            <td><p> <span class="art-button-wrapper"> <span class="art-button-l"> </span> <span class="art-button-r"> </span> <a class="readon art-button" href="javascript:void(0)">Chia sẻ</a> </span> </p></td>
                          </tr>
                         
                        </table>
                        <?php foreach($ds_status as $status) {
										   ?>
                        <div class="group_status" name="group_status[]">
                          <div class="wall">
                            <div class="f_img"><img src="<?php echo get_img($uid);?>" /></div>
                            <div class="f_ten"><a href="sdfdsfds"><b><?php echo $user['name']?> </b></a></div>
                            <div class="f_truong"><a href="sdfdsfds"><?php echo date("Y-m-d h:j:s", $status['time']);?> </a></div>
                          </div>
                          <div class="cleared"></div>
                          <div class="status"><?php echo $status['message']?></div>
                          <div class="cleared"></div>
                          <div class="bo_nut" style="background:#EDEFF4;width:600px;border-bottom:1px solid #FFF"><a>like</a><input type="button" onclick="get_cm(<?php echo $stt; ?>)" value="Comment" /></div>
                          
                          <?php $sl_cm = $dt_xl_comment->so_luong($status['status_id']); 
						  if($sl_cm > 3) {
						  ?>
                          
                          <div class="view" style="background:#EDEFF4;width:600px;border-bottom:1px solid #FFF"><input type="button" onclick="view_all_cm(<?php echo $stt; ?>)" value="view all <?php echo $sl_cm;?> comments" /></div>
                          
                           <?php 
						  }
						  		$sl = max($sl_cm-3,0);
							  	$ds_comment = $dt_xl_comment->danh_sach($status['status_id'],$sl,$sl_cm);
								$ds_all_comment = $dt_xl_comment->danh_sach($status['status_id'],0,$sl_cm);
								?>
                                <input type="hidden" name="ds_cmt[]" value="<?php echo show_comment($ds_all_comment)?>" /><div name="ins_before[]"></div>
								<div  name="comment_tb[]"><?php show_comment($ds_comment); ?></div>
                                
                          <div name="ins_new_comm[]"></div>
                          <div><input type="hidden" name="dsstt[]" value="<?php echo $status['status_id'];?>" /><input type="text" name="comment[]" value="" /> <button type="button" onclick="bt_cm(<?php echo $stt; ?>)" name="bt_comment[]">binh luan</button></div>
                          
                          
                          <div class="cleared"></div>
                        </div>
                        <?php  $stt++; } ?>
                      </div>
                      <div class="cleared"></div>
                      <!-- sdsf --> 
                    </div>
                    <div class="cleared"></div>
                  </div>
                </div>
                <div class="cleared"></div>
              </div>
              <div class="art-layout-cell art-sidebar2">
                <?php 
						  $pos = 'right';
						  sidebar($pos);
						  ?>
                <div class="cleared"></div>
              </div>
            </div>
          </div>
          <div class="cleared"></div>
          <div class="cleared"></div>
        </div>
      </div>
      <div class="art-footer">
        <div class="art-footer-t"></div>
        <div class="art-footer-body"> <a href="#" class="art-rss-tag-icon" title="RSS"></a>
          <div class="art-footer-text">
            <p><a href="#">Link1</a> | <a href="#">Link2</a> | <a href="#">Link3</a></p>
            <p>Copyright © 2012. All Rights Reserved.</p>
            <div class="cleared"></div>
            <p class="art-page-footer"><a href="http://www.artisteer.com/?p=joomla_templates">Joomla template</a> created with Artisteer.</p>
          </div>
          <div class="cleared"></div>
        </div>
      </div>
    </div>
  </div>
</div>
</body>
</html>
<?php 
	function show_comment($ds_comment){
		foreach($ds_comment as $comment) {
			
			echo "<div class='wall'>";
				echo "<div class='f_img'><img src='".get_img($comment['from_uid'])."' /></div>";
				echo "<div class='f_ten'><a href='sdfdsfds'><b>". get_name($comment['from_uid']). " </b></a>". $comment['text']. "</div>";
				echo "<div class='f_truong'><a href='sdfdsfds'>" . $comment['time']."</a></div>";
		 	echo "</div>";
        }
	}
?>